package testJdk.io;

import cn.hutool.core.io.FileUtil;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName : com.io.TestFile
 * @Description : 类描述
 * Created by cyd on 2020-12-07 23:19:15
 */
public class TestFileRead {
    public static void main(String[] args) {

        File file = new File("a.txt");

        System.out.println(file.exists());//false

        //如果不存在就创建
        if (!file.exists()) {
//            File dir = file.getParentFile();
//            if (!dir.exists()) {
//                dir.mkdirs();
//            }
            try {
                boolean flag = file.createNewFile();
                System.out.println(flag);//true
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testFile() {

        // 获取resources下的文件
        URL resourceUrl = getClass().getClassLoader().getResource("data/info");
        File file = new File(resourceUrl.getPath());
        System.out.println(file);
        System.out.println("-----------------------------------------");

        // 读取文件成字符串
        byte[] bytes = FileUtil.readBytes(file);
        String data = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(data);
    }
}
